package com.wangxinenpu.springbootdemo.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.wangxinenpu.springbootdemo.config.security.LoginVo;
import com.wangxinenpu.springbootdemo.config.security.XlResource;
import com.wangxinenpu.springbootdemo.dataobject.vo.task.AjaxResult;
import com.wangxinenpu.springbootdemo.service.ISysUserService;
import com.wangxinenpu.springbootdemo.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "后台用户管理")
@RestController
@RequestMapping("/ucenter/xl-user")
@CrossOrigin
public class SysUserController {

    @Autowired
    private LoginService xlUserService;
    @Autowired
    private ISysUserService iSysUserService;
    @Value("${jwt.tokenHead}")
    private String tokenHead;



    @ApiOperation(value = "登录")
    @PostMapping(value = "login")
    public AjaxResult login(@RequestBody LoginVo loginVo) {
        String token = xlUserService.login(loginVo.getUsername(), loginVo.getPassword());
        if (StringUtils.isBlank(token)) {
            return AjaxResult.error("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return AjaxResult.success(tokenMap);
    }

    @ApiOperation("获取用户所有可访问的资源")
    @GetMapping(value = "/resource/{userId}")
    public R getResourceList(@PathVariable Long userId) {
        List<XlResource> permissionList = iSysUserService.getResourceList(userId);
        return R.ok(permissionList);
    }


    @ApiOperation("获取用户信息")
    @GetMapping(value = "/info")
    public R info(@PathVariable Long userId) {
        List<XlResource> permissionList = iSysUserService.getResourceList(userId);
        return R.ok(permissionList);
    }
}

